# Name:    CFile.py
# Date:    Tue Mar  9 15:30:27 CET 2010
# Purpose: The class handles file descriptor
# Def:     CFile(filename, mode)
# Inputs:  filename - the name of file to work with
#          mode - the mode in which the file is opened [r, w]

import os
import sys

####################################################################################################

class CFile():
	'''The class handles file descriptor
	'''
	__fileName = None
	__mode = None
	__fileDesc = None

#---------------------------------------------------------------------------------------------------

	def __init__(self, name, mode):
		self.__fileName = name
		self.__mode = mode

		self.__load()

#---------------------------------------------------------------------------------------------------

	def __del__(self):
		if self.__fileDesc != None:
			self.__fileDesc.close()

#---------------------------------------------------------------------------------------------------

	def __load(self):
		try:
			self.__fileDesc = open(self.__fileName, self.__mode)
		except IOError, e:
			# TODO: Create Logger class
			self.__fileDesc = None
			raise e
			#sys.exit(1)

#---------------------------------------------------------------------------------------------------
	
	def getFileName(self):
		return self.__fileName

#---------------------------------------------------------------------------------------------------
	
	def getFileDesc(self):
		return self.__fileDesc

#---------------------------------------------------------------------------------------------------
	
	def begin(self):
		self.__fileDesc.seek(0, 0)

#---------------------------------------------------------------------------------------------------
	
	def sync(self):
		self.__fileDesc.flush()
		os.fsync(self.__fileDesc.fileno())

####################################################################################################
